var textarea2b = document.getElementById("textarea2b");
var submitBtn = document.getElementById("submitBtn");
var noteList = document.getElementById("noteList");

var noteArray = getNoteArray();
for (var i = 0; i < noteArray.length; i++) {
	var noteInfo = noteArray[i];
	addToDOM(noteInfo);
}

submitBtn.onclick = function() {
	//alert("If U cn rd ts, U wl gt a jb w hi p.");
	createNote();

	var noteArray = getNoteArray();
	var newNote = noteArray[noteArray.length-1];
	addToDOM(newNote);
	window.location.reload();
}

function createNote() {
	var noteArray = getNoteArray();
	var value = textarea2b.value;
	var year = new Date().getFullYear(),
		month = new Date().getMonth(),
		day = new Date().getDay(),
		hour = new Date().getHours(),
		minute = new Date().getMinutes();
	minute = minute >= 10 ? minute : "0"+minute;
	var time = year + "-" + month + "-" + day + " " + hour + ":" + minute;
	//var time = new Date().toString();
	var noteInfo = {
		"value": value,
		"time": time
	}
	noteArray.push(noteInfo);
	localStorage.setItem("noteArray", JSON.stringify(noteArray));
}

function getNoteArray() {
	var noteArray = localStorage.getItem("noteArray");

	if (!noteArray) {
		noteArray = [];
		localStorage.setItem("noteArray", JSON.stringify(noteArray));
	} else {
		noteArray = JSON.parse(noteArray);
	}

	return noteArray;
}

function addToDOM(noteInfo) {
	var noteNode = document.createElement("li");
	noteNode.innerHTML = "<p>" + noteInfo.value + "</p>" 
	+ "<p class=\"ui-li-aside\"><strong>" + noteInfo.time + "</strong>PM</p>";
	noteList.appendChild(noteNode);
}